<?php

/*
 * litchicms
 * Copyright (C) 2010-7-1 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");


$can_action = array(
    'list', 'edit', 'editpost',
    'del', 'recycle', 'torecycle', 'tolist',
    'manytorecycle', 'manytolist', 'manydel',
    'com');  //com使用组件的处理器

if (empty($_POST['action']))
    !empty($RT['v']['collac']) && $_POST['action'] = $RT['v']['collac'];

if (!in_array($_POST['action'], $can_action)) {
    $_POST['action'] = 'list';
}


switch ($_POST['action']) {
    case 'list':
        // select handle
        empty($RT['v']['sh']) && $RT['v']['sh'] = 'default'; //sh=select handle
        !function_exists('list_select_' . $RT['v']['sh']) && $RT['v']['sh'] = 'default';
        if ($RT['v']['sh'] == 'default') {


            function list_select_default() {
                global $_PAGE, $RT;
                $array = array('partid' => $GLOBALS['COLLECTION_ID'], 'pageid' => 'a', 'num' => !empty($RT['v']['pagena']) ? $RT['v']['pagena'] : 10, 'isbg' => TRUE);
                $rs = COLL_list($array);
                return $rs;
            }

        }

        $COLL_DATA = call_user_func('list_select_' . $RT['v']['sh']);

        break;
    case 'edit':

        require dirname(__FILE__) . '/list/' . $COLLECTION_ENAME . '.Validation.php';

        $COLLECTION_DATA = array();
        isset($RT['v']['dataid']) && $DATA_ID = intval($RT['v']['dataid']);
        empty($DATA_ID) && $DATA_ID = brk_insert('_coll_' . $COLLECTION_ENAME, array('id' => 'null'));

        $RECORD_ID = $DATA_ID;

        $ONE_DATA = brk_select_one('_coll_' . $COLLECTION_ENAME, "*", "WHERE id=" . $DATA_ID);

        foreach ($COLLECTION_conf as $key => $value) {
            foreach ($value as $v) {
                $COLLECTION_DATA[$v['name']] = $v['buildUiParam'] ? call_user_func('Component_buildUiParam_' . $v['name'], $COLLECTION_ID, $ONE_DATA) : array("collection_value" => $ONE_DATA[$v['name']]);
            }
        }

        require dirname(__FILE__) . '/list/' . $COLLECTION_ENAME . '.Control.php';

        break;

    case 'editpost':
        !key_exists('field', $_POST) && exit("NO POST FILED");
        !is_array($_POST['field']) && exit("POST FIELD NOT IS A ARRAY");
        !key_exists('id', $_POST['field']) && exit("NO POST FIELD ID");
        $DATA_ID = intval($_POST['field']['id']);
        $DATA_ID == 0 && exit("ID岂能等于0..");
        $RECORD_ID = $DATA_ID;
        !($ONE_DATA = brk_select_one('_coll_' . $COLLECTION_ENAME, "*", "WHERE id=" . $RECORD_ID)) && exit("ID....");

        require dirname(__FILE__) . '/list/' . $COLLECTION_ENAME . '.Validation.php';

        $ValidationOK = TRUE;
        foreach ($BRK_GLOBAL['ComponentValidationFunction'] as $v) {
            if (($temp = call_user_func($v)) !== TRUE) {
                eMsg(0, $temp); //array(1,bgpre("collac-list")
                $ValidationOK = FALSE;
            }
        }
        if (!$ValidationOK) {
            echo "<script language='javascript' type='text/javascript'>window.parent.list_edit_response(" . json_encode($GLOBALS['BRK_MSG']) . ")</script>";
            exit;
        }

//        $COLLECTION_DATA=brk_select_one('_coll_' . $COLLECTION_ENAME, "*", "WHERE id=".$RECORD_ID);

        $COLLECTION_DATA = array();
        foreach ($COLLECTION_conf as $key => $value) {
            foreach ($value as $v) {
                if (!$v['columns']) {
                    continue;
                }
                $columnArr = !empty($v['buildField']) ? call_user_func('Component_buildField_' . $v['name']) : array($v['name'] => $_POST['field'][$v['name']]);
                $COLLECTION_DATA = array_merge($COLLECTION_DATA, $columnArr);
            }
        }

        if (brk_update('_coll_' . $COLLECTION_ENAME, $COLLECTION_DATA, "WHERE id=" . $RECORD_ID)) {
            eMsg(1, "编辑内容成功!", array(1, bgpre("collac-list")));
        }

        $EndingOK = TRUE;
        foreach ($BRK_GLOBAL['ComponentEndingFunction'] as $v) {
            if (($temp = call_user_func($v)) !== TRUE) {
                //eMsg(0, $temp);
                $EndingOK = FALSE;
            }
        }

        echo "<script language='javascript' type='text/javascript'>window.parent.list_edit_response(" . json_encode($GLOBALS['BRK_MSG']) . ")</script>";
        exit;

        break;
    case 'del':
        if (empty($RT['v']['dataid']) || ($DATA_ID = intval($RT['v']['dataid'])) == 0) {
            eMsg(0, "参数不合法啊!", array(1, "?" . bgpre("collac-list")));
            $_POST['action'] = 'error';
        }

        //删除数据
        brk_delete($COLLECTION_TABLENAME, "WHERE id=" . $DATA_ID);

        //调用组件的删除函数
        foreach ($BRK_GLOBAL['ComponentDeleteOneDateFunction'] as $v) {
            call_user_func($v, $DATA_ID);
        }

        //删除缓存..


        eMsg(1, "OK!", array(1, "?" . bgpre("collac-list")));
        $_POST['action'] = 'error';
        break;
    case 'manydel':
        if (!_filter_id()) {
            break;
        }

        if (!empty($_POST['field']['coll_selected'])) {
            foreach ($_POST['field']['coll_selected'] as $value) {
                //删除数据
                brk_delete($COLLECTION_TABLENAME, "WHERE id=" . $value);
                //调用组件的删除函数
                if (!empty($BRK_GLOBAL['ComponentDeleteOneDateFunction'])) {
                    foreach ($BRK_GLOBAL['ComponentDeleteOneDateFunction'] as $v) {
                        call_user_func($v, $value);
                    }
                }

                //删除缓存..
            }
        }

        eMsg(1, "OK!", array(1, "?" . bgpre("collac-list")));
        $_POST['action'] = 'error';
        break;
    case 'recycle':
        $num = current(brk_select_one($GLOBALS['COLLECTION_TABLENAME'], 'count(id) as num', "WHERE status=0"));
        $page_info = COLL_PAGE_info('a', !empty($RT['v']['pagena']) ? $RT['v']['pagena'] : 10, $num, FALSE);

        $COLL_DATA = array();
        $rs = brk_select($GLOBALS['COLLECTION_TABLENAME'], '*', "WHERE status=0  LIMIT " . ($page_info['current'] - 1) * $page_info['oenPageNum'] . "," . $page_info['oenPageNum'] . " ");
        while ($row = $Db->fetch_array($rs)) {
            $COLL_DATA[] = $row;
        }

        break;
    case 'torecycle':
        if (empty($RT['v']['dataid']) || ($DATA_ID = intval($RT['v']['dataid'])) == 0) {
            eMsg(0, "参数不合法啊!", array(1, "?" . bgpre("collac-list")));
            $_POST['action'] = 'error';
        }

        //
        brk_update($COLLECTION_TABLENAME, array('status' => 0), "WHERE id=" . $DATA_ID);
        eMsg(1, "OK!", array(1, "?" . bgpre("collac-list")));
        $_POST['action'] = 'error';

        break;

    case 'manytorecycle':
        if (!_filter_id()) {
            break;
        }


        if (!empty($_POST['field']['coll_selected'])) {
            foreach ($_POST['field']['coll_selected'] as $value) {
                brk_update($COLLECTION_TABLENAME, array('status' => 0), "WHERE id=" . $value);
            }
        }

        eMsg(1, "OK!", array(1, "?" . bgpre("collac-list")));
        $_POST['action'] = 'error';
        break;

    case 'tolist':
        if (empty($RT['v']['dataid']) || ($DATA_ID = intval($RT['v']['dataid'])) == 0) {
            eMsg(0, "参数不合法啊!", array(1, "?" . bgpre("collac-list")));
            $_POST['action'] = 'error';
        }

        //
        brk_update($COLLECTION_TABLENAME, array('status' => 1), "WHERE id=" . $DATA_ID);
        eMsg(1, "OK!", array(1, "?" . bgpre("collac-list")));
        $_POST['action'] = 'error';
        break;
    case 'manytolist':
        if (!_filter_id()) {
            break;
        }


        if (!empty($_POST['field']['coll_selected'])) {
            foreach ($_POST['field']['coll_selected'] as $value) {
                brk_update($COLLECTION_TABLENAME, array('status' => 1), "WHERE id=" . $value);
            }
        }

        eMsg(1, "OK!", array(1, "?" . bgpre("collac-list")));
        $_POST['action'] = 'error';
        break;
    case 'com':
        if (empty($RT['v']['com'])) {
            eMsg(0, "!", array(1, "?" . bgpre("collac-list")));
            $_POST['action'] = 'error';
            break;
        }
        eMsg(0, null, array(1, "?" . bgpre("collac-list")));
        foreach ($COLLECTION_conf as $v) {
            foreach ($v as $vv) {
                if ($vv['name'] == $RT['v']['com']) {
                    if (is_file($file = SYSTEM . 'include/Component/' . $vv['where'] . '/' . $vv['name'] . '/call.php')) {
                        if (require $file) {
                            eMsg(1, "操作成功!", array(1, "?" . bgpre("collac-list")));
                        }
                        break 2;
                    }
                }
            }
        }

        $_POST['action'] = 'error';
        break;
    default:
        break;
}

//id 过滤
function _filter_id() {
    if (empty($_POST['field']['coll_selected']) || !is_array($_POST['field']['coll_selected'])) {
        eMsg(0, "参数不合法啊!", array(1, "?" . bgpre("collac-list")));
        $_POST['action'] = 'error';
        return FALSE;
    }

    foreach ($_POST['field']['coll_selected'] as &$value) {
        $value = intval($value);
    }
    unset($value);

    $_POST['field']['coll_selected'] = array_filter($_POST['field']['coll_selected']);

    return TRUE;
}